<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Formsets &mdash; Django 1.7.8.dev20150401230226 documentation</title>
    
    <link rel="stylesheet" href="../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../',
        VERSION:     '1.7.8.dev20150401230226',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <link rel="top" title="Django 1.7.8.dev20150401230226 documentation" href="../../index.html" />
    <link rel="up" title="Working with forms" href="index.html" />
    <link rel="next" title="Creating forms from models" href="modelforms.html" />
    <link rel="prev" title="Working with forms" href="index.html" />



 
<script type="text/javascript" src="../../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../../ref/templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);
</script>


  </head>
  <body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../index.html">Django 1.7.8.dev20150401230226 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../index.html">Home</a>  |
        <a title="Table of contents" href="../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../genindex.html">Index</a>  |
        <a title="Module index" href="../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    &laquo; <a href="index.html" title="Working with forms">previous</a>
     |
    <a href="../index.html" title="Using Django" accesskey="U">up</a>
   |
    <a href="modelforms.html" title="Creating forms from models">next</a> &raquo;</div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="topics-forms-formsets">
            
  <div class="section" id="s-formsets">
<span id="s-id1"></span><span id="formsets"></span><span id="id1"></span><h1>Formsets<a class="headerlink" href="#formsets" title="Permalink to this headline">¶</a></h1>
<span class="target" id="module-django.forms.formsets"></span><dl class="class">
<dt id="django.forms.formsets.BaseFormSet">
<em class="property">class </em><tt class="descname">BaseFormSet</tt><a class="reference internal" href="../../_modules/django/forms/formsets.html#BaseFormSet"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.forms.formsets.BaseFormSet" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>A formset is a layer of abstraction to work with multiple forms on the same
page. It can be best compared to a data grid. Let&#8217;s say you have the following
form:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django</span> <span class="kn">import</span> <span class="n">forms</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">ArticleForm</span><span class="p">(</span><span class="n">forms</span><span class="o">.</span><span class="n">Form</span><span class="p">):</span>
<span class="gp">... </span>    <span class="n">title</span> <span class="o">=</span> <span class="n">forms</span><span class="o">.</span><span class="n">CharField</span><span class="p">()</span>
<span class="gp">... </span>    <span class="n">pub_date</span> <span class="o">=</span> <span class="n">forms</span><span class="o">.</span><span class="n">DateField</span><span class="p">()</span>
</pre></div>
</div>
<p>You might want to allow the user to create several articles at once. To create
a formset out of an <tt class="docutils literal"><span class="pre">ArticleForm</span></tt> you would do:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.forms.formsets</span> <span class="kn">import</span> <span class="n">formset_factory</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ArticleFormSet</span> <span class="o">=</span> <span class="n">formset_factory</span><span class="p">(</span><span class="n">ArticleForm</span><span class="p">)</span>
</pre></div>
</div>
<p>You now have created a formset named <tt class="docutils literal"><span class="pre">ArticleFormSet</span></tt>. The formset gives you
the ability to iterate over the forms in the formset and display them as you
would with a regular form:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">formset</span> <span class="o">=</span> <span class="n">ArticleFormSet</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">form</span> <span class="ow">in</span> <span class="n">formset</span><span class="p">:</span>
<span class="gp">... </span>    <span class="k">print</span><span class="p">(</span><span class="n">form</span><span class="o">.</span><span class="n">as_table</span><span class="p">())</span>
<span class="go">&lt;tr&gt;&lt;th&gt;&lt;label for=&quot;id_form-0-title&quot;&gt;Title:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input type=&quot;text&quot; name=&quot;form-0-title&quot; id=&quot;id_form-0-title&quot; /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
<span class="go">&lt;tr&gt;&lt;th&gt;&lt;label for=&quot;id_form-0-pub_date&quot;&gt;Pub date:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input type=&quot;text&quot; name=&quot;form-0-pub_date&quot; id=&quot;id_form-0-pub_date&quot; /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
</pre></div>
</div>
<p>As you can see it only displayed one empty form. The number of empty forms
that is displayed is controlled by the <tt class="docutils literal"><span class="pre">extra</span></tt> parameter. By default,
<a class="reference internal" href="../../ref/forms/formsets.html#django.forms.formsets.formset_factory" title="django.forms.formsets.formset_factory"><tt class="xref py py-func docutils literal"><span class="pre">formset_factory()</span></tt></a> defines one extra form; the
following example will display two blank forms:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">ArticleFormSet</span> <span class="o">=</span> <span class="n">formset_factory</span><span class="p">(</span><span class="n">ArticleForm</span><span class="p">,</span> <span class="n">extra</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span>
</pre></div>
</div>
<p>Iterating over the <tt class="docutils literal"><span class="pre">formset</span></tt> will render the forms in the order they were
created. You can change this order by providing an alternate implementation for
the <tt class="docutils literal"><span class="pre">__iter__()</span></tt> method.</p>
<p>Formsets can also be indexed into, which returns the corresponding form. If you
override <tt class="docutils literal"><span class="pre">__iter__</span></tt>, you will need to also override <tt class="docutils literal"><span class="pre">__getitem__</span></tt> to have
matching behavior.</p>
<div class="section" id="s-using-initial-data-with-a-formset">
<span id="s-formsets-initial-data"></span><span id="using-initial-data-with-a-formset"></span><span id="formsets-initial-data"></span><h2>Using initial data with a formset<a class="headerlink" href="#using-initial-data-with-a-formset" title="Permalink to this headline">¶</a></h2>
<p>Initial data is what drives the main usability of a formset. As shown above
you can define the number of extra forms. What this means is that you are
telling the formset how many additional forms to show in addition to the
number of forms it generates from the initial data. Let&#8217;s take a look at an
example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">datetime</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.forms.formsets</span> <span class="kn">import</span> <span class="n">formset_factory</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">myapp.forms</span> <span class="kn">import</span> <span class="n">ArticleForm</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ArticleFormSet</span> <span class="o">=</span> <span class="n">formset_factory</span><span class="p">(</span><span class="n">ArticleForm</span><span class="p">,</span> <span class="n">extra</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">formset</span> <span class="o">=</span> <span class="n">ArticleFormSet</span><span class="p">(</span><span class="n">initial</span><span class="o">=</span><span class="p">[</span>
<span class="gp">... </span>    <span class="p">{</span><span class="s">&#39;title&#39;</span><span class="p">:</span> <span class="s">u&#39;Django is now open source&#39;</span><span class="p">,</span>
<span class="gp">... </span>     <span class="s">&#39;pub_date&#39;</span><span class="p">:</span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="o">.</span><span class="n">today</span><span class="p">(),}</span>
<span class="gp">... </span><span class="p">])</span>

<span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">form</span> <span class="ow">in</span> <span class="n">formset</span><span class="p">:</span>
<span class="gp">... </span>    <span class="k">print</span><span class="p">(</span><span class="n">form</span><span class="o">.</span><span class="n">as_table</span><span class="p">())</span>
<span class="go">&lt;tr&gt;&lt;th&gt;&lt;label for=&quot;id_form-0-title&quot;&gt;Title:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input type=&quot;text&quot; name=&quot;form-0-title&quot; value=&quot;Django is now open source&quot; id=&quot;id_form-0-title&quot; /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
<span class="go">&lt;tr&gt;&lt;th&gt;&lt;label for=&quot;id_form-0-pub_date&quot;&gt;Pub date:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input type=&quot;text&quot; name=&quot;form-0-pub_date&quot; value=&quot;2008-05-12&quot; id=&quot;id_form-0-pub_date&quot; /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
<span class="go">&lt;tr&gt;&lt;th&gt;&lt;label for=&quot;id_form-1-title&quot;&gt;Title:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input type=&quot;text&quot; name=&quot;form-1-title&quot; id=&quot;id_form-1-title&quot; /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
<span class="go">&lt;tr&gt;&lt;th&gt;&lt;label for=&quot;id_form-1-pub_date&quot;&gt;Pub date:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input type=&quot;text&quot; name=&quot;form-1-pub_date&quot; id=&quot;id_form-1-pub_date&quot; /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
<span class="go">&lt;tr&gt;&lt;th&gt;&lt;label for=&quot;id_form-2-title&quot;&gt;Title:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input type=&quot;text&quot; name=&quot;form-2-title&quot; id=&quot;id_form-2-title&quot; /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
<span class="go">&lt;tr&gt;&lt;th&gt;&lt;label for=&quot;id_form-2-pub_date&quot;&gt;Pub date:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input type=&quot;text&quot; name=&quot;form-2-pub_date&quot; id=&quot;id_form-2-pub_date&quot; /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
</pre></div>
</div>
<p>There are now a total of three forms showing above. One for the initial data
that was passed in and two extra forms. Also note that we are passing in a
list of dictionaries as the initial data.</p>
<div class="admonition seealso">
<p class="first admonition-title">See also</p>
<p class="last"><a class="reference internal" href="modelforms.html#model-formsets"><em>Creating formsets from models with model formsets</em></a>.</p>
</div>
</div>
<div class="section" id="s-limiting-the-maximum-number-of-forms">
<span id="s-formsets-max-num"></span><span id="limiting-the-maximum-number-of-forms"></span><span id="formsets-max-num"></span><h2>Limiting the maximum number of forms<a class="headerlink" href="#limiting-the-maximum-number-of-forms" title="Permalink to this headline">¶</a></h2>
<p>The <tt class="docutils literal"><span class="pre">max_num</span></tt> parameter to <a class="reference internal" href="../../ref/forms/formsets.html#django.forms.formsets.formset_factory" title="django.forms.formsets.formset_factory"><tt class="xref py py-func docutils literal"><span class="pre">formset_factory()</span></tt></a>
gives you the ability to limit the number of forms the formset will display:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.forms.formsets</span> <span class="kn">import</span> <span class="n">formset_factory</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">myapp.forms</span> <span class="kn">import</span> <span class="n">ArticleForm</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ArticleFormSet</span> <span class="o">=</span> <span class="n">formset_factory</span><span class="p">(</span><span class="n">ArticleForm</span><span class="p">,</span> <span class="n">extra</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">max_num</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">formset</span> <span class="o">=</span> <span class="n">ArticleFormSet</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">form</span> <span class="ow">in</span> <span class="n">formset</span><span class="p">:</span>
<span class="gp">... </span>    <span class="k">print</span><span class="p">(</span><span class="n">form</span><span class="o">.</span><span class="n">as_table</span><span class="p">())</span>
<span class="go">&lt;tr&gt;&lt;th&gt;&lt;label for=&quot;id_form-0-title&quot;&gt;Title:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input type=&quot;text&quot; name=&quot;form-0-title&quot; id=&quot;id_form-0-title&quot; /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
<span class="go">&lt;tr&gt;&lt;th&gt;&lt;label for=&quot;id_form-0-pub_date&quot;&gt;Pub date:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input type=&quot;text&quot; name=&quot;form-0-pub_date&quot; id=&quot;id_form-0-pub_date&quot; /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
</pre></div>
</div>
<p>If the value of <tt class="docutils literal"><span class="pre">max_num</span></tt> is greater than the number of existing items in the
initial data, up to <tt class="docutils literal"><span class="pre">extra</span></tt> additional blank forms will be added to the
formset, so long as the total number of forms does not exceed <tt class="docutils literal"><span class="pre">max_num</span></tt>. For
example, if <tt class="docutils literal"><span class="pre">extra=2</span></tt> and <tt class="docutils literal"><span class="pre">max_num=2</span></tt> and the formset is initialized with
one <tt class="docutils literal"><span class="pre">initial</span></tt> item, a form for the initial item and one blank form will be
displayed.</p>
<p>If the number of items in the initial data exceeds <tt class="docutils literal"><span class="pre">max_num</span></tt>, all initial
data forms will be displayed regardless of the value of <tt class="docutils literal"><span class="pre">max_num</span></tt> and no
extra forms will be displayed. For example, if <tt class="docutils literal"><span class="pre">extra=3</span></tt> and <tt class="docutils literal"><span class="pre">max_num=1</span></tt>
and the formset is initialized with two initial items, two forms with the
initial data will be displayed.</p>
<p>A <tt class="docutils literal"><span class="pre">max_num</span></tt> value of <tt class="docutils literal"><span class="pre">None</span></tt> (the default) puts a high limit on the number
of forms displayed (1000). In practice this is equivalent to no limit.</p>
<p>By default, <tt class="docutils literal"><span class="pre">max_num</span></tt> only affects how many forms are displayed and does not
affect validation.  If <tt class="docutils literal"><span class="pre">validate_max=True</span></tt> is passed to the
<a class="reference internal" href="../../ref/forms/formsets.html#django.forms.formsets.formset_factory" title="django.forms.formsets.formset_factory"><tt class="xref py py-func docutils literal"><span class="pre">formset_factory()</span></tt></a>, then <tt class="docutils literal"><span class="pre">max_num</span></tt> will affect
validation.  See <a class="reference internal" href="#validate-max"><em>Validating the number of forms in a formset</em></a>.</p>
<div class="versionchanged">
<span class="title">Changed in Django 1.6:</span> <p>The <tt class="docutils literal"><span class="pre">validate_max</span></tt> parameter was added to
<a class="reference internal" href="../../ref/forms/formsets.html#django.forms.formsets.formset_factory" title="django.forms.formsets.formset_factory"><tt class="xref py py-func docutils literal"><span class="pre">formset_factory()</span></tt></a>.  Also, the behavior of
<tt class="docutils literal"><span class="pre">FormSet</span></tt> was brought in line with that of <tt class="docutils literal"><span class="pre">ModelFormSet</span></tt> so that it
displays initial data regardless of <tt class="docutils literal"><span class="pre">max_num</span></tt>.</p>
</div>
</div>
<div class="section" id="s-formset-validation">
<span id="formset-validation"></span><h2>Formset validation<a class="headerlink" href="#formset-validation" title="Permalink to this headline">¶</a></h2>
<p>Validation with a formset is almost identical to a regular <tt class="docutils literal"><span class="pre">Form</span></tt>. There is
an <tt class="docutils literal"><span class="pre">is_valid</span></tt> method on the formset to provide a convenient way to validate
all forms in the formset:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.forms.formsets</span> <span class="kn">import</span> <span class="n">formset_factory</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">myapp.forms</span> <span class="kn">import</span> <span class="n">ArticleForm</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ArticleFormSet</span> <span class="o">=</span> <span class="n">formset_factory</span><span class="p">(</span><span class="n">ArticleForm</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">data</span> <span class="o">=</span> <span class="p">{</span>
<span class="gp">... </span>    <span class="s">&#39;form-TOTAL_FORMS&#39;</span><span class="p">:</span> <span class="s">u&#39;1&#39;</span><span class="p">,</span>
<span class="gp">... </span>    <span class="s">&#39;form-INITIAL_FORMS&#39;</span><span class="p">:</span> <span class="s">u&#39;0&#39;</span><span class="p">,</span>
<span class="gp">... </span>    <span class="s">&#39;form-MAX_NUM_FORMS&#39;</span><span class="p">:</span> <span class="s">u&#39;&#39;</span><span class="p">,</span>
<span class="gp">... </span><span class="p">}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">formset</span> <span class="o">=</span> <span class="n">ArticleFormSet</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">formset</span><span class="o">.</span><span class="n">is_valid</span><span class="p">()</span>
<span class="go">True</span>
</pre></div>
</div>
<p>We passed in no data to the formset which is resulting in a valid form. The
formset is smart enough to ignore extra forms that were not changed. If we
provide an invalid article:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">data</span> <span class="o">=</span> <span class="p">{</span>
<span class="gp">... </span>    <span class="s">&#39;form-TOTAL_FORMS&#39;</span><span class="p">:</span> <span class="s">u&#39;2&#39;</span><span class="p">,</span>
<span class="gp">... </span>    <span class="s">&#39;form-INITIAL_FORMS&#39;</span><span class="p">:</span> <span class="s">u&#39;0&#39;</span><span class="p">,</span>
<span class="gp">... </span>    <span class="s">&#39;form-MAX_NUM_FORMS&#39;</span><span class="p">:</span> <span class="s">u&#39;&#39;</span><span class="p">,</span>
<span class="gp">... </span>    <span class="s">&#39;form-0-title&#39;</span><span class="p">:</span> <span class="s">u&#39;Test&#39;</span><span class="p">,</span>
<span class="gp">... </span>    <span class="s">&#39;form-0-pub_date&#39;</span><span class="p">:</span> <span class="s">u&#39;1904-06-16&#39;</span><span class="p">,</span>
<span class="gp">... </span>    <span class="s">&#39;form-1-title&#39;</span><span class="p">:</span> <span class="s">u&#39;Test&#39;</span><span class="p">,</span>
<span class="gp">... </span>    <span class="s">&#39;form-1-pub_date&#39;</span><span class="p">:</span> <span class="s">u&#39;&#39;</span><span class="p">,</span> <span class="c"># &lt;-- this date is missing but required</span>
<span class="gp">... </span><span class="p">}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">formset</span> <span class="o">=</span> <span class="n">ArticleFormSet</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">formset</span><span class="o">.</span><span class="n">is_valid</span><span class="p">()</span>
<span class="go">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">formset</span><span class="o">.</span><span class="n">errors</span>
<span class="go">[{}, {&#39;pub_date&#39;: [u&#39;This field is required.&#39;]}]</span>
</pre></div>
</div>
<p>As we can see, <tt class="docutils literal"><span class="pre">formset.errors</span></tt> is a list whose entries correspond to the
forms in the formset. Validation was performed for each of the two forms, and
the expected error message appears for the second item.</p>
<dl class="method">
<dt id="django.forms.formsets.BaseFormSet.total_error_count">
<tt class="descclassname">BaseFormSet.</tt><tt class="descname">total_error_count</tt>()<a class="reference internal" href="../../_modules/django/forms/formsets.html#BaseFormSet.total_error_count"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.forms.formsets.BaseFormSet.total_error_count" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<div class="versionadded">
<span class="title">New in Django 1.6.</span> </div>
<p>To check how many errors there are in the formset, we can use the
<tt class="docutils literal"><span class="pre">total_error_count</span></tt> method:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="c"># Using the previous example</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">formset</span><span class="o">.</span><span class="n">errors</span>
<span class="go">[{}, {&#39;pub_date&#39;: [u&#39;This field is required.&#39;]}]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">len</span><span class="p">(</span><span class="n">formset</span><span class="o">.</span><span class="n">errors</span><span class="p">)</span>
<span class="go">2</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">formset</span><span class="o">.</span><span class="n">total_error_count</span><span class="p">()</span>
<span class="go">1</span>
</pre></div>
</div>
<p>We can also check if form data differs from the initial data (i.e. the form was
sent without any data):</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">data</span> <span class="o">=</span> <span class="p">{</span>
<span class="gp">... </span>    <span class="s">&#39;form-TOTAL_FORMS&#39;</span><span class="p">:</span> <span class="s">u&#39;1&#39;</span><span class="p">,</span>
<span class="gp">... </span>    <span class="s">&#39;form-INITIAL_FORMS&#39;</span><span class="p">:</span> <span class="s">u&#39;0&#39;</span><span class="p">,</span>
<span class="gp">... </span>    <span class="s">&#39;form-MAX_NUM_FORMS&#39;</span><span class="p">:</span> <span class="s">u&#39;&#39;</span><span class="p">,</span>
<span class="gp">... </span>    <span class="s">&#39;form-0-title&#39;</span><span class="p">:</span> <span class="s">u&#39;&#39;</span><span class="p">,</span>
<span class="gp">... </span>    <span class="s">&#39;form-0-pub_date&#39;</span><span class="p">:</span> <span class="s">u&#39;&#39;</span><span class="p">,</span>
<span class="gp">... </span><span class="p">}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">formset</span> <span class="o">=</span> <span class="n">ArticleFormSet</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">formset</span><span class="o">.</span><span class="n">has_changed</span><span class="p">()</span>
<span class="go">False</span>
</pre></div>
</div>
<div class="section" id="s-understanding-the-managementform">
<span id="s-id2"></span><span id="understanding-the-managementform"></span><span id="id2"></span><h3>Understanding the ManagementForm<a class="headerlink" href="#understanding-the-managementform" title="Permalink to this headline">¶</a></h3>
<p>You may have noticed the additional data (<tt class="docutils literal"><span class="pre">form-TOTAL_FORMS</span></tt>,
<tt class="docutils literal"><span class="pre">form-INITIAL_FORMS</span></tt> and <tt class="docutils literal"><span class="pre">form-MAX_NUM_FORMS</span></tt>) that was required
in the formset&#8217;s data above. This data is required for the
<tt class="docutils literal"><span class="pre">ManagementForm</span></tt>. This form is used by the formset to manage the
collection of forms contained in the formset. If you don&#8217;t provide
this management data, an exception will be raised:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">data</span> <span class="o">=</span> <span class="p">{</span>
<span class="gp">... </span>    <span class="s">&#39;form-0-title&#39;</span><span class="p">:</span> <span class="s">u&#39;Test&#39;</span><span class="p">,</span>
<span class="gp">... </span>    <span class="s">&#39;form-0-pub_date&#39;</span><span class="p">:</span> <span class="s">u&#39;&#39;</span><span class="p">,</span>
<span class="gp">... </span><span class="p">}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">formset</span> <span class="o">=</span> <span class="n">ArticleFormSet</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
<span class="c">...</span>
<span class="gr">django.forms.utils.ValidationError</span>: <span class="n">[u&#39;ManagementForm data is missing or has been tampered with&#39;]</span>
</pre></div>
</div>
<p>It is used to keep track of how many form instances are being displayed. If
you are adding new forms via JavaScript, you should increment the count fields
in this form as well. On the other hand, if you are using JavaScript to allow
deletion of existing objects, then you need to ensure the ones being removed
are properly marked for deletion by including <tt class="docutils literal"><span class="pre">form-#-DELETE</span></tt> in the <tt class="docutils literal"><span class="pre">POST</span></tt>
data. It is expected that all forms are present in the <tt class="docutils literal"><span class="pre">POST</span></tt> data regardless.</p>
<p>The management form is available as an attribute of the formset
itself. When rendering a formset in a template, you can include all
the management data by rendering <tt class="docutils literal"><span class="pre">{{</span> <span class="pre">my_formset.management_form</span> <span class="pre">}}</span></tt>
(substituting the name of your formset as appropriate).</p>
</div>
<div class="section" id="s-total-form-count-and-initial-form-count">
<span id="total-form-count-and-initial-form-count"></span><h3><tt class="docutils literal"><span class="pre">total_form_count</span></tt> and <tt class="docutils literal"><span class="pre">initial_form_count</span></tt><a class="headerlink" href="#total-form-count-and-initial-form-count" title="Permalink to this headline">¶</a></h3>
<p><tt class="docutils literal"><span class="pre">BaseFormSet</span></tt> has a couple of methods that are closely related to the
<tt class="docutils literal"><span class="pre">ManagementForm</span></tt>, <tt class="docutils literal"><span class="pre">total_form_count</span></tt> and <tt class="docutils literal"><span class="pre">initial_form_count</span></tt>.</p>
<p><tt class="docutils literal"><span class="pre">total_form_count</span></tt> returns the total number of forms in this formset.
<tt class="docutils literal"><span class="pre">initial_form_count</span></tt> returns the number of forms in the formset that were
pre-filled, and is also used to determine how many forms are required. You
will probably never need to override either of these methods, so please be
sure you understand what they do before doing so.</p>
</div>
<div class="section" id="s-empty-form">
<span id="empty-form"></span><h3><tt class="docutils literal"><span class="pre">empty_form</span></tt><a class="headerlink" href="#empty-form" title="Permalink to this headline">¶</a></h3>
<p><tt class="docutils literal"><span class="pre">BaseFormSet</span></tt> provides an additional attribute <tt class="docutils literal"><span class="pre">empty_form</span></tt> which returns
a form instance with a prefix of <tt class="docutils literal"><span class="pre">__prefix__</span></tt> for easier use in dynamic
forms with JavaScript.</p>
</div>
<div class="section" id="s-custom-formset-validation">
<span id="custom-formset-validation"></span><h3>Custom formset validation<a class="headerlink" href="#custom-formset-validation" title="Permalink to this headline">¶</a></h3>
<p>A formset has a <tt class="docutils literal"><span class="pre">clean</span></tt> method similar to the one on a <tt class="docutils literal"><span class="pre">Form</span></tt> class. This
is where you define your own validation that works at the formset level:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.forms.formsets</span> <span class="kn">import</span> <span class="n">BaseFormSet</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.forms.formsets</span> <span class="kn">import</span> <span class="n">formset_factory</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">myapp.forms</span> <span class="kn">import</span> <span class="n">ArticleForm</span>

<span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">BaseArticleFormSet</span><span class="p">(</span><span class="n">BaseFormSet</span><span class="p">):</span>
<span class="gp">... </span>    <span class="k">def</span> <span class="nf">clean</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="gp">... </span>        <span class="sd">&quot;&quot;&quot;Checks that no two articles have the same title.&quot;&quot;&quot;</span>
<span class="gp">... </span>        <span class="k">if</span> <span class="nb">any</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">errors</span><span class="p">):</span>
<span class="gp">... </span>            <span class="c"># Don&#39;t bother validating the formset unless each form is valid on its own</span>
<span class="gp">... </span>            <span class="k">return</span>
<span class="gp">... </span>        <span class="n">titles</span> <span class="o">=</span> <span class="p">[]</span>
<span class="gp">... </span>        <span class="k">for</span> <span class="n">form</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">forms</span><span class="p">:</span>
<span class="gp">... </span>            <span class="n">title</span> <span class="o">=</span> <span class="n">form</span><span class="o">.</span><span class="n">cleaned_data</span><span class="p">[</span><span class="s">&#39;title&#39;</span><span class="p">]</span>
<span class="gp">... </span>            <span class="k">if</span> <span class="n">title</span> <span class="ow">in</span> <span class="n">titles</span><span class="p">:</span>
<span class="gp">... </span>                <span class="k">raise</span> <span class="n">forms</span><span class="o">.</span><span class="n">ValidationError</span><span class="p">(</span><span class="s">&quot;Articles in a set must have distinct titles.&quot;</span><span class="p">)</span>
<span class="gp">... </span>            <span class="n">titles</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">title</span><span class="p">)</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">ArticleFormSet</span> <span class="o">=</span> <span class="n">formset_factory</span><span class="p">(</span><span class="n">ArticleForm</span><span class="p">,</span> <span class="n">formset</span><span class="o">=</span><span class="n">BaseArticleFormSet</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">data</span> <span class="o">=</span> <span class="p">{</span>
<span class="gp">... </span>    <span class="s">&#39;form-TOTAL_FORMS&#39;</span><span class="p">:</span> <span class="s">u&#39;2&#39;</span><span class="p">,</span>
<span class="gp">... </span>    <span class="s">&#39;form-INITIAL_FORMS&#39;</span><span class="p">:</span> <span class="s">u&#39;0&#39;</span><span class="p">,</span>
<span class="gp">... </span>    <span class="s">&#39;form-MAX_NUM_FORMS&#39;</span><span class="p">:</span> <span class="s">u&#39;&#39;</span><span class="p">,</span>
<span class="gp">... </span>    <span class="s">&#39;form-0-title&#39;</span><span class="p">:</span> <span class="s">u&#39;Test&#39;</span><span class="p">,</span>
<span class="gp">... </span>    <span class="s">&#39;form-0-pub_date&#39;</span><span class="p">:</span> <span class="s">u&#39;1904-06-16&#39;</span><span class="p">,</span>
<span class="gp">... </span>    <span class="s">&#39;form-1-title&#39;</span><span class="p">:</span> <span class="s">u&#39;Test&#39;</span><span class="p">,</span>
<span class="gp">... </span>    <span class="s">&#39;form-1-pub_date&#39;</span><span class="p">:</span> <span class="s">u&#39;1912-06-23&#39;</span><span class="p">,</span>
<span class="gp">... </span><span class="p">}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">formset</span> <span class="o">=</span> <span class="n">ArticleFormSet</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">formset</span><span class="o">.</span><span class="n">is_valid</span><span class="p">()</span>
<span class="go">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">formset</span><span class="o">.</span><span class="n">errors</span>
<span class="go">[{}, {}]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">formset</span><span class="o">.</span><span class="n">non_form_errors</span><span class="p">()</span>
<span class="go">[u&#39;Articles in a set must have distinct titles.&#39;]</span>
</pre></div>
</div>
<p>The formset <tt class="docutils literal"><span class="pre">clean</span></tt> method is called after all the <tt class="docutils literal"><span class="pre">Form.clean</span></tt> methods
have been called. The errors will be found using the <tt class="docutils literal"><span class="pre">non_form_errors()</span></tt>
method on the formset.</p>
</div>
</div>
<div class="section" id="s-validating-the-number-of-forms-in-a-formset">
<span id="s-validate-max"></span><span id="validating-the-number-of-forms-in-a-formset"></span><span id="validate-max"></span><h2>Validating the number of forms in a formset<a class="headerlink" href="#validating-the-number-of-forms-in-a-formset" title="Permalink to this headline">¶</a></h2>
<p>Django provides a couple ways to validate the minimum or maximum number of
submitted forms. Applications which need more customizable validation of the
number of forms should use custom formset validation.</p>
<div class="section" id="s-id3">
<span id="id3"></span><h3><tt class="docutils literal"><span class="pre">validate_max</span></tt><a class="headerlink" href="#id3" title="Permalink to this headline">¶</a></h3>
<p>If <tt class="docutils literal"><span class="pre">validate_max=True</span></tt> is passed to
<a class="reference internal" href="../../ref/forms/formsets.html#django.forms.formsets.formset_factory" title="django.forms.formsets.formset_factory"><tt class="xref py py-func docutils literal"><span class="pre">formset_factory()</span></tt></a>, validation will also check
that the number of forms in the data set, minus those marked for
deletion, is less than or equal to <tt class="docutils literal"><span class="pre">max_num</span></tt>.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.forms.formsets</span> <span class="kn">import</span> <span class="n">formset_factory</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">myapp.forms</span> <span class="kn">import</span> <span class="n">ArticleForm</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ArticleFormSet</span> <span class="o">=</span> <span class="n">formset_factory</span><span class="p">(</span><span class="n">ArticleForm</span><span class="p">,</span> <span class="n">max_num</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">validate_max</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">data</span> <span class="o">=</span> <span class="p">{</span>
<span class="gp">... </span>    <span class="s">&#39;form-TOTAL_FORMS&#39;</span><span class="p">:</span> <span class="s">u&#39;2&#39;</span><span class="p">,</span>
<span class="gp">... </span>    <span class="s">&#39;form-INITIAL_FORMS&#39;</span><span class="p">:</span> <span class="s">u&#39;0&#39;</span><span class="p">,</span>
<span class="gp">... </span>    <span class="s">&#39;form-MIN_NUM_FORMS&#39;</span><span class="p">:</span> <span class="s">u&#39;&#39;</span><span class="p">,</span>
<span class="gp">... </span>    <span class="s">&#39;form-MAX_NUM_FORMS&#39;</span><span class="p">:</span> <span class="s">u&#39;&#39;</span><span class="p">,</span>
<span class="gp">... </span>    <span class="s">&#39;form-0-title&#39;</span><span class="p">:</span> <span class="s">u&#39;Test&#39;</span><span class="p">,</span>
<span class="gp">... </span>    <span class="s">&#39;form-0-pub_date&#39;</span><span class="p">:</span> <span class="s">u&#39;1904-06-16&#39;</span><span class="p">,</span>
<span class="gp">... </span>    <span class="s">&#39;form-1-title&#39;</span><span class="p">:</span> <span class="s">u&#39;Test 2&#39;</span><span class="p">,</span>
<span class="gp">... </span>    <span class="s">&#39;form-1-pub_date&#39;</span><span class="p">:</span> <span class="s">u&#39;1912-06-23&#39;</span><span class="p">,</span>
<span class="gp">... </span><span class="p">}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">formset</span> <span class="o">=</span> <span class="n">ArticleFormSet</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">formset</span><span class="o">.</span><span class="n">is_valid</span><span class="p">()</span>
<span class="go">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">formset</span><span class="o">.</span><span class="n">errors</span>
<span class="go">[{}, {}]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">formset</span><span class="o">.</span><span class="n">non_form_errors</span><span class="p">()</span>
<span class="go">[u&#39;Please submit 1 or fewer forms.&#39;]</span>
</pre></div>
</div>
<p><tt class="docutils literal"><span class="pre">validate_max=True</span></tt> validates against <tt class="docutils literal"><span class="pre">max_num</span></tt> strictly even if
<tt class="docutils literal"><span class="pre">max_num</span></tt> was exceeded because the amount of initial data supplied was
excessive.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Regardless of <tt class="docutils literal"><span class="pre">validate_max</span></tt>, if the number of forms in a data set
exceeds <tt class="docutils literal"><span class="pre">max_num</span></tt> by more than 1000, then the form will fail to validate
as if <tt class="docutils literal"><span class="pre">validate_max</span></tt> were set, and additionally only the first 1000
forms above <tt class="docutils literal"><span class="pre">max_num</span></tt> will be validated.  The remainder will be
truncated entirely.  This is to protect against memory exhaustion attacks
using forged POST requests.</p>
</div>
<div class="versionchanged">
<span class="title">Changed in Django 1.6:</span> <p>The <tt class="docutils literal"><span class="pre">validate_max</span></tt> parameter was added to
<a class="reference internal" href="../../ref/forms/formsets.html#django.forms.formsets.formset_factory" title="django.forms.formsets.formset_factory"><tt class="xref py py-func docutils literal"><span class="pre">formset_factory()</span></tt></a>.</p>
</div>
</div>
<div class="section" id="s-validate-min">
<span id="validate-min"></span><h3><tt class="docutils literal"><span class="pre">validate_min</span></tt><a class="headerlink" href="#validate-min" title="Permalink to this headline">¶</a></h3>
<div class="versionadded">
<span class="title">New in Django 1.7.</span> </div>
<p>If <tt class="docutils literal"><span class="pre">validate_min=True</span></tt> is passed to
<a class="reference internal" href="../../ref/forms/formsets.html#django.forms.formsets.formset_factory" title="django.forms.formsets.formset_factory"><tt class="xref py py-func docutils literal"><span class="pre">formset_factory()</span></tt></a>, validation will also check
that the number of forms in the data set, minus those marked for
deletion, is greater than or equal to <tt class="docutils literal"><span class="pre">min_num</span></tt>.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.forms.formsets</span> <span class="kn">import</span> <span class="n">formset_factory</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">myapp.forms</span> <span class="kn">import</span> <span class="n">ArticleForm</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ArticleFormSet</span> <span class="o">=</span> <span class="n">formset_factory</span><span class="p">(</span><span class="n">ArticleForm</span><span class="p">,</span> <span class="n">min_num</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span> <span class="n">validate_min</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">data</span> <span class="o">=</span> <span class="p">{</span>
<span class="gp">... </span>    <span class="s">&#39;form-TOTAL_FORMS&#39;</span><span class="p">:</span> <span class="s">u&#39;2&#39;</span><span class="p">,</span>
<span class="gp">... </span>    <span class="s">&#39;form-INITIAL_FORMS&#39;</span><span class="p">:</span> <span class="s">u&#39;0&#39;</span><span class="p">,</span>
<span class="gp">... </span>    <span class="s">&#39;form-MIN_NUM_FORMS&#39;</span><span class="p">:</span> <span class="s">u&#39;&#39;</span><span class="p">,</span>
<span class="gp">... </span>    <span class="s">&#39;form-MAX_NUM_FORMS&#39;</span><span class="p">:</span> <span class="s">u&#39;&#39;</span><span class="p">,</span>
<span class="gp">... </span>    <span class="s">&#39;form-0-title&#39;</span><span class="p">:</span> <span class="s">u&#39;Test&#39;</span><span class="p">,</span>
<span class="gp">... </span>    <span class="s">&#39;form-0-pub_date&#39;</span><span class="p">:</span> <span class="s">u&#39;1904-06-16&#39;</span><span class="p">,</span>
<span class="gp">... </span>    <span class="s">&#39;form-1-title&#39;</span><span class="p">:</span> <span class="s">u&#39;Test 2&#39;</span><span class="p">,</span>
<span class="gp">... </span>    <span class="s">&#39;form-1-pub_date&#39;</span><span class="p">:</span> <span class="s">u&#39;1912-06-23&#39;</span><span class="p">,</span>
<span class="gp">... </span><span class="p">}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">formset</span> <span class="o">=</span> <span class="n">ArticleFormSet</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">formset</span><span class="o">.</span><span class="n">is_valid</span><span class="p">()</span>
<span class="go">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">formset</span><span class="o">.</span><span class="n">errors</span>
<span class="go">[{}, {}]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">formset</span><span class="o">.</span><span class="n">non_form_errors</span><span class="p">()</span>
<span class="go">[u&#39;Please submit 3 or more forms.&#39;]</span>
</pre></div>
</div>
<div class="versionchanged">
<span class="title">Changed in Django 1.7:</span> <p>The <tt class="docutils literal"><span class="pre">min_num</span></tt> and <tt class="docutils literal"><span class="pre">validate_min</span></tt> parameters were added to
<a class="reference internal" href="../../ref/forms/formsets.html#django.forms.formsets.formset_factory" title="django.forms.formsets.formset_factory"><tt class="xref py py-func docutils literal"><span class="pre">formset_factory()</span></tt></a>.</p>
</div>
</div>
</div>
<div class="section" id="s-dealing-with-ordering-and-deletion-of-forms">
<span id="dealing-with-ordering-and-deletion-of-forms"></span><h2>Dealing with ordering and deletion of forms<a class="headerlink" href="#dealing-with-ordering-and-deletion-of-forms" title="Permalink to this headline">¶</a></h2>
<p>The <a class="reference internal" href="../../ref/forms/formsets.html#django.forms.formsets.formset_factory" title="django.forms.formsets.formset_factory"><tt class="xref py py-func docutils literal"><span class="pre">formset_factory()</span></tt></a> provides two optional
parameters <tt class="docutils literal"><span class="pre">can_order</span></tt> and <tt class="docutils literal"><span class="pre">can_delete</span></tt> to help with ordering of forms in
formsets and deletion of forms from a formset.</p>
<div class="section" id="s-can-order">
<span id="can-order"></span><h3><tt class="docutils literal"><span class="pre">can_order</span></tt><a class="headerlink" href="#can-order" title="Permalink to this headline">¶</a></h3>
<dl class="attribute">
<dt id="django.forms.formsets.BaseFormSet.can_order">
<tt class="descclassname">BaseFormSet.</tt><tt class="descname">can_order</tt><a class="headerlink" href="#django.forms.formsets.BaseFormSet.can_order" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>Default: <tt class="docutils literal"><span class="pre">False</span></tt></p>
<p>Lets you create a formset with the ability to order:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.forms.formsets</span> <span class="kn">import</span> <span class="n">formset_factory</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">myapp.forms</span> <span class="kn">import</span> <span class="n">ArticleForm</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ArticleFormSet</span> <span class="o">=</span> <span class="n">formset_factory</span><span class="p">(</span><span class="n">ArticleForm</span><span class="p">,</span> <span class="n">can_order</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">formset</span> <span class="o">=</span> <span class="n">ArticleFormSet</span><span class="p">(</span><span class="n">initial</span><span class="o">=</span><span class="p">[</span>
<span class="gp">... </span>    <span class="p">{</span><span class="s">&#39;title&#39;</span><span class="p">:</span> <span class="s">u&#39;Article #1&#39;</span><span class="p">,</span> <span class="s">&#39;pub_date&#39;</span><span class="p">:</span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">(</span><span class="mi">2008</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">10</span><span class="p">)},</span>
<span class="gp">... </span>    <span class="p">{</span><span class="s">&#39;title&#39;</span><span class="p">:</span> <span class="s">u&#39;Article #2&#39;</span><span class="p">,</span> <span class="s">&#39;pub_date&#39;</span><span class="p">:</span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">(</span><span class="mi">2008</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">11</span><span class="p">)},</span>
<span class="gp">... </span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">form</span> <span class="ow">in</span> <span class="n">formset</span><span class="p">:</span>
<span class="gp">... </span>    <span class="k">print</span><span class="p">(</span><span class="n">form</span><span class="o">.</span><span class="n">as_table</span><span class="p">())</span>
<span class="go">&lt;tr&gt;&lt;th&gt;&lt;label for=&quot;id_form-0-title&quot;&gt;Title:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input type=&quot;text&quot; name=&quot;form-0-title&quot; value=&quot;Article #1&quot; id=&quot;id_form-0-title&quot; /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
<span class="go">&lt;tr&gt;&lt;th&gt;&lt;label for=&quot;id_form-0-pub_date&quot;&gt;Pub date:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input type=&quot;text&quot; name=&quot;form-0-pub_date&quot; value=&quot;2008-05-10&quot; id=&quot;id_form-0-pub_date&quot; /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
<span class="go">&lt;tr&gt;&lt;th&gt;&lt;label for=&quot;id_form-0-ORDER&quot;&gt;Order:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input type=&quot;number&quot; name=&quot;form-0-ORDER&quot; value=&quot;1&quot; id=&quot;id_form-0-ORDER&quot; /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
<span class="go">&lt;tr&gt;&lt;th&gt;&lt;label for=&quot;id_form-1-title&quot;&gt;Title:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input type=&quot;text&quot; name=&quot;form-1-title&quot; value=&quot;Article #2&quot; id=&quot;id_form-1-title&quot; /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
<span class="go">&lt;tr&gt;&lt;th&gt;&lt;label for=&quot;id_form-1-pub_date&quot;&gt;Pub date:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input type=&quot;text&quot; name=&quot;form-1-pub_date&quot; value=&quot;2008-05-11&quot; id=&quot;id_form-1-pub_date&quot; /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
<span class="go">&lt;tr&gt;&lt;th&gt;&lt;label for=&quot;id_form-1-ORDER&quot;&gt;Order:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input type=&quot;number&quot; name=&quot;form-1-ORDER&quot; value=&quot;2&quot; id=&quot;id_form-1-ORDER&quot; /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
<span class="go">&lt;tr&gt;&lt;th&gt;&lt;label for=&quot;id_form-2-title&quot;&gt;Title:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input type=&quot;text&quot; name=&quot;form-2-title&quot; id=&quot;id_form-2-title&quot; /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
<span class="go">&lt;tr&gt;&lt;th&gt;&lt;label for=&quot;id_form-2-pub_date&quot;&gt;Pub date:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input type=&quot;text&quot; name=&quot;form-2-pub_date&quot; id=&quot;id_form-2-pub_date&quot; /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
<span class="go">&lt;tr&gt;&lt;th&gt;&lt;label for=&quot;id_form-2-ORDER&quot;&gt;Order:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input type=&quot;number&quot; name=&quot;form-2-ORDER&quot; id=&quot;id_form-2-ORDER&quot; /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
</pre></div>
</div>
<p>This adds an additional field to each form. This new field is named <tt class="docutils literal"><span class="pre">ORDER</span></tt>
and is an <tt class="docutils literal"><span class="pre">forms.IntegerField</span></tt>. For the forms that came from the initial
data it automatically assigned them a numeric value. Let&#8217;s look at what will
happen when the user changes these values:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">data</span> <span class="o">=</span> <span class="p">{</span>
<span class="gp">... </span>    <span class="s">&#39;form-TOTAL_FORMS&#39;</span><span class="p">:</span> <span class="s">u&#39;3&#39;</span><span class="p">,</span>
<span class="gp">... </span>    <span class="s">&#39;form-INITIAL_FORMS&#39;</span><span class="p">:</span> <span class="s">u&#39;2&#39;</span><span class="p">,</span>
<span class="gp">... </span>    <span class="s">&#39;form-MAX_NUM_FORMS&#39;</span><span class="p">:</span> <span class="s">u&#39;&#39;</span><span class="p">,</span>
<span class="gp">... </span>    <span class="s">&#39;form-0-title&#39;</span><span class="p">:</span> <span class="s">u&#39;Article #1&#39;</span><span class="p">,</span>
<span class="gp">... </span>    <span class="s">&#39;form-0-pub_date&#39;</span><span class="p">:</span> <span class="s">u&#39;2008-05-10&#39;</span><span class="p">,</span>
<span class="gp">... </span>    <span class="s">&#39;form-0-ORDER&#39;</span><span class="p">:</span> <span class="s">u&#39;2&#39;</span><span class="p">,</span>
<span class="gp">... </span>    <span class="s">&#39;form-1-title&#39;</span><span class="p">:</span> <span class="s">u&#39;Article #2&#39;</span><span class="p">,</span>
<span class="gp">... </span>    <span class="s">&#39;form-1-pub_date&#39;</span><span class="p">:</span> <span class="s">u&#39;2008-05-11&#39;</span><span class="p">,</span>
<span class="gp">... </span>    <span class="s">&#39;form-1-ORDER&#39;</span><span class="p">:</span> <span class="s">u&#39;1&#39;</span><span class="p">,</span>
<span class="gp">... </span>    <span class="s">&#39;form-2-title&#39;</span><span class="p">:</span> <span class="s">u&#39;Article #3&#39;</span><span class="p">,</span>
<span class="gp">... </span>    <span class="s">&#39;form-2-pub_date&#39;</span><span class="p">:</span> <span class="s">u&#39;2008-05-01&#39;</span><span class="p">,</span>
<span class="gp">... </span>    <span class="s">&#39;form-2-ORDER&#39;</span><span class="p">:</span> <span class="s">u&#39;0&#39;</span><span class="p">,</span>
<span class="gp">... </span><span class="p">}</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">formset</span> <span class="o">=</span> <span class="n">ArticleFormSet</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">initial</span><span class="o">=</span><span class="p">[</span>
<span class="gp">... </span>    <span class="p">{</span><span class="s">&#39;title&#39;</span><span class="p">:</span> <span class="s">u&#39;Article #1&#39;</span><span class="p">,</span> <span class="s">&#39;pub_date&#39;</span><span class="p">:</span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">(</span><span class="mi">2008</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">10</span><span class="p">)},</span>
<span class="gp">... </span>    <span class="p">{</span><span class="s">&#39;title&#39;</span><span class="p">:</span> <span class="s">u&#39;Article #2&#39;</span><span class="p">,</span> <span class="s">&#39;pub_date&#39;</span><span class="p">:</span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">(</span><span class="mi">2008</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">11</span><span class="p">)},</span>
<span class="gp">... </span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">formset</span><span class="o">.</span><span class="n">is_valid</span><span class="p">()</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">form</span> <span class="ow">in</span> <span class="n">formset</span><span class="o">.</span><span class="n">ordered_forms</span><span class="p">:</span>
<span class="gp">... </span>    <span class="k">print</span><span class="p">(</span><span class="n">form</span><span class="o">.</span><span class="n">cleaned_data</span><span class="p">)</span>
<span class="go">{&#39;pub_date&#39;: datetime.date(2008, 5, 1), &#39;ORDER&#39;: 0, &#39;title&#39;: u&#39;Article #3&#39;}</span>
<span class="go">{&#39;pub_date&#39;: datetime.date(2008, 5, 11), &#39;ORDER&#39;: 1, &#39;title&#39;: u&#39;Article #2&#39;}</span>
<span class="go">{&#39;pub_date&#39;: datetime.date(2008, 5, 10), &#39;ORDER&#39;: 2, &#39;title&#39;: u&#39;Article #1&#39;}</span>
</pre></div>
</div>
</div>
<div class="section" id="s-can-delete">
<span id="can-delete"></span><h3><tt class="docutils literal"><span class="pre">can_delete</span></tt><a class="headerlink" href="#can-delete" title="Permalink to this headline">¶</a></h3>
<dl class="attribute">
<dt id="django.forms.formsets.BaseFormSet.can_delete">
<tt class="descclassname">BaseFormSet.</tt><tt class="descname">can_delete</tt><a class="headerlink" href="#django.forms.formsets.BaseFormSet.can_delete" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>Default: <tt class="docutils literal"><span class="pre">False</span></tt></p>
<p>Lets you create a formset with the ability to select forms for deletion:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.forms.formsets</span> <span class="kn">import</span> <span class="n">formset_factory</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">myapp.forms</span> <span class="kn">import</span> <span class="n">ArticleForm</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ArticleFormSet</span> <span class="o">=</span> <span class="n">formset_factory</span><span class="p">(</span><span class="n">ArticleForm</span><span class="p">,</span> <span class="n">can_delete</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">formset</span> <span class="o">=</span> <span class="n">ArticleFormSet</span><span class="p">(</span><span class="n">initial</span><span class="o">=</span><span class="p">[</span>
<span class="gp">... </span>    <span class="p">{</span><span class="s">&#39;title&#39;</span><span class="p">:</span> <span class="s">u&#39;Article #1&#39;</span><span class="p">,</span> <span class="s">&#39;pub_date&#39;</span><span class="p">:</span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">(</span><span class="mi">2008</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">10</span><span class="p">)},</span>
<span class="gp">... </span>    <span class="p">{</span><span class="s">&#39;title&#39;</span><span class="p">:</span> <span class="s">u&#39;Article #2&#39;</span><span class="p">,</span> <span class="s">&#39;pub_date&#39;</span><span class="p">:</span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">(</span><span class="mi">2008</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">11</span><span class="p">)},</span>
<span class="gp">... </span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">form</span> <span class="ow">in</span> <span class="n">formset</span><span class="p">:</span>
<span class="go">....    print(form.as_table())</span>
<span class="go">&lt;input type=&quot;hidden&quot; name=&quot;form-TOTAL_FORMS&quot; value=&quot;3&quot; id=&quot;id_form-TOTAL_FORMS&quot; /&gt;&lt;input type=&quot;hidden&quot; name=&quot;form-INITIAL_FORMS&quot; value=&quot;2&quot; id=&quot;id_form-INITIAL_FORMS&quot; /&gt;&lt;input type=&quot;hidden&quot; name=&quot;form-MAX_NUM_FORMS&quot; id=&quot;id_form-MAX_NUM_FORMS&quot; /&gt;</span>
<span class="go">&lt;tr&gt;&lt;th&gt;&lt;label for=&quot;id_form-0-title&quot;&gt;Title:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input type=&quot;text&quot; name=&quot;form-0-title&quot; value=&quot;Article #1&quot; id=&quot;id_form-0-title&quot; /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
<span class="go">&lt;tr&gt;&lt;th&gt;&lt;label for=&quot;id_form-0-pub_date&quot;&gt;Pub date:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input type=&quot;text&quot; name=&quot;form-0-pub_date&quot; value=&quot;2008-05-10&quot; id=&quot;id_form-0-pub_date&quot; /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
<span class="go">&lt;tr&gt;&lt;th&gt;&lt;label for=&quot;id_form-0-DELETE&quot;&gt;Delete:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input type=&quot;checkbox&quot; name=&quot;form-0-DELETE&quot; id=&quot;id_form-0-DELETE&quot; /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
<span class="go">&lt;tr&gt;&lt;th&gt;&lt;label for=&quot;id_form-1-title&quot;&gt;Title:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input type=&quot;text&quot; name=&quot;form-1-title&quot; value=&quot;Article #2&quot; id=&quot;id_form-1-title&quot; /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
<span class="go">&lt;tr&gt;&lt;th&gt;&lt;label for=&quot;id_form-1-pub_date&quot;&gt;Pub date:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input type=&quot;text&quot; name=&quot;form-1-pub_date&quot; value=&quot;2008-05-11&quot; id=&quot;id_form-1-pub_date&quot; /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
<span class="go">&lt;tr&gt;&lt;th&gt;&lt;label for=&quot;id_form-1-DELETE&quot;&gt;Delete:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input type=&quot;checkbox&quot; name=&quot;form-1-DELETE&quot; id=&quot;id_form-1-DELETE&quot; /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
<span class="go">&lt;tr&gt;&lt;th&gt;&lt;label for=&quot;id_form-2-title&quot;&gt;Title:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input type=&quot;text&quot; name=&quot;form-2-title&quot; id=&quot;id_form-2-title&quot; /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
<span class="go">&lt;tr&gt;&lt;th&gt;&lt;label for=&quot;id_form-2-pub_date&quot;&gt;Pub date:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input type=&quot;text&quot; name=&quot;form-2-pub_date&quot; id=&quot;id_form-2-pub_date&quot; /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
<span class="go">&lt;tr&gt;&lt;th&gt;&lt;label for=&quot;id_form-2-DELETE&quot;&gt;Delete:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input type=&quot;checkbox&quot; name=&quot;form-2-DELETE&quot; id=&quot;id_form-2-DELETE&quot; /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
</pre></div>
</div>
<p>Similar to <tt class="docutils literal"><span class="pre">can_order</span></tt> this adds a new field to each form named <tt class="docutils literal"><span class="pre">DELETE</span></tt>
and is a <tt class="docutils literal"><span class="pre">forms.BooleanField</span></tt>. When data comes through marking any of the
delete fields you can access them with <tt class="docutils literal"><span class="pre">deleted_forms</span></tt>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">data</span> <span class="o">=</span> <span class="p">{</span>
<span class="gp">... </span>    <span class="s">&#39;form-TOTAL_FORMS&#39;</span><span class="p">:</span> <span class="s">u&#39;3&#39;</span><span class="p">,</span>
<span class="gp">... </span>    <span class="s">&#39;form-INITIAL_FORMS&#39;</span><span class="p">:</span> <span class="s">u&#39;2&#39;</span><span class="p">,</span>
<span class="gp">... </span>    <span class="s">&#39;form-MAX_NUM_FORMS&#39;</span><span class="p">:</span> <span class="s">u&#39;&#39;</span><span class="p">,</span>
<span class="gp">... </span>    <span class="s">&#39;form-0-title&#39;</span><span class="p">:</span> <span class="s">u&#39;Article #1&#39;</span><span class="p">,</span>
<span class="gp">... </span>    <span class="s">&#39;form-0-pub_date&#39;</span><span class="p">:</span> <span class="s">u&#39;2008-05-10&#39;</span><span class="p">,</span>
<span class="gp">... </span>    <span class="s">&#39;form-0-DELETE&#39;</span><span class="p">:</span> <span class="s">u&#39;on&#39;</span><span class="p">,</span>
<span class="gp">... </span>    <span class="s">&#39;form-1-title&#39;</span><span class="p">:</span> <span class="s">u&#39;Article #2&#39;</span><span class="p">,</span>
<span class="gp">... </span>    <span class="s">&#39;form-1-pub_date&#39;</span><span class="p">:</span> <span class="s">u&#39;2008-05-11&#39;</span><span class="p">,</span>
<span class="gp">... </span>    <span class="s">&#39;form-1-DELETE&#39;</span><span class="p">:</span> <span class="s">u&#39;&#39;</span><span class="p">,</span>
<span class="gp">... </span>    <span class="s">&#39;form-2-title&#39;</span><span class="p">:</span> <span class="s">u&#39;&#39;</span><span class="p">,</span>
<span class="gp">... </span>    <span class="s">&#39;form-2-pub_date&#39;</span><span class="p">:</span> <span class="s">u&#39;&#39;</span><span class="p">,</span>
<span class="gp">... </span>    <span class="s">&#39;form-2-DELETE&#39;</span><span class="p">:</span> <span class="s">u&#39;&#39;</span><span class="p">,</span>
<span class="gp">... </span><span class="p">}</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">formset</span> <span class="o">=</span> <span class="n">ArticleFormSet</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">initial</span><span class="o">=</span><span class="p">[</span>
<span class="gp">... </span>    <span class="p">{</span><span class="s">&#39;title&#39;</span><span class="p">:</span> <span class="s">u&#39;Article #1&#39;</span><span class="p">,</span> <span class="s">&#39;pub_date&#39;</span><span class="p">:</span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">(</span><span class="mi">2008</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">10</span><span class="p">)},</span>
<span class="gp">... </span>    <span class="p">{</span><span class="s">&#39;title&#39;</span><span class="p">:</span> <span class="s">u&#39;Article #2&#39;</span><span class="p">,</span> <span class="s">&#39;pub_date&#39;</span><span class="p">:</span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">(</span><span class="mi">2008</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">11</span><span class="p">)},</span>
<span class="gp">... </span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="p">[</span><span class="n">form</span><span class="o">.</span><span class="n">cleaned_data</span> <span class="k">for</span> <span class="n">form</span> <span class="ow">in</span> <span class="n">formset</span><span class="o">.</span><span class="n">deleted_forms</span><span class="p">]</span>
<span class="go">[{&#39;DELETE&#39;: True, &#39;pub_date&#39;: datetime.date(2008, 5, 10), &#39;title&#39;: u&#39;Article #1&#39;}]</span>
</pre></div>
</div>
<p>If you are using a <a class="reference internal" href="modelforms.html#django.forms.models.BaseModelFormSet" title="django.forms.models.BaseModelFormSet"><tt class="xref py py-class docutils literal"><span class="pre">ModelFormSet</span></tt></a>,
model instances for deleted forms will be deleted when you call
<tt class="docutils literal"><span class="pre">formset.save()</span></tt>.</p>
<div class="versionchanged">
<span class="title">Changed in Django 1.7:</span> <p>If you call <tt class="docutils literal"><span class="pre">formset.save(commit=False)</span></tt>, objects will not be deleted
automatically.  You&#8217;ll need to call <tt class="docutils literal"><span class="pre">delete()</span></tt> on each of the
<a class="reference internal" href="modelforms.html#django.forms.models.BaseModelFormSet.deleted_objects" title="django.forms.models.BaseModelFormSet.deleted_objects"><tt class="xref py py-attr docutils literal"><span class="pre">formset.deleted_objects</span></tt></a> to actually delete
them:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">instances</span> <span class="o">=</span> <span class="n">formset</span><span class="o">.</span><span class="n">save</span><span class="p">(</span><span class="n">commit</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">obj</span> <span class="ow">in</span> <span class="n">formset</span><span class="o">.</span><span class="n">deleted_objects</span><span class="p">:</span>
<span class="gp">... </span>    <span class="n">obj</span><span class="o">.</span><span class="n">delete</span><span class="p">()</span>
</pre></div>
</div>
<p>If you want to maintain backwards compatibility with Django 1.6 and earlier,
you can do something like this:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="k">try</span><span class="p">:</span>
<span class="gp">&gt;&gt;&gt; </span>    <span class="c"># For Django 1.7+</span>
<span class="gp">&gt;&gt;&gt; </span>    <span class="k">for</span> <span class="n">obj</span> <span class="ow">in</span> <span class="n">formset</span><span class="o">.</span><span class="n">deleted_objects</span><span class="p">:</span>
<span class="gp">&gt;&gt;&gt; </span>        <span class="n">obj</span><span class="o">.</span><span class="n">delete</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">except</span> <span class="ne">AssertionError</span><span class="p">:</span>
<span class="gp">&gt;&gt;&gt; </span>    <span class="c"># Django 1.6 and earlier already deletes the objects, trying to</span>
<span class="gp">&gt;&gt;&gt; </span>    <span class="c"># delete them a second time raises an AssertionError.</span>
<span class="gp">&gt;&gt;&gt; </span>    <span class="k">pass</span>
</pre></div>
</div>
</div>
<p>On the other hand, if you are using a plain <tt class="docutils literal"><span class="pre">FormSet</span></tt>, it&#8217;s up to you to
handle <tt class="docutils literal"><span class="pre">formset.deleted_forms</span></tt>, perhaps in your formset&#8217;s <tt class="docutils literal"><span class="pre">save()</span></tt> method,
as there&#8217;s no general notion of what it means to delete a form.</p>
</div>
</div>
<div class="section" id="s-adding-additional-fields-to-a-formset">
<span id="adding-additional-fields-to-a-formset"></span><h2>Adding additional fields to a formset<a class="headerlink" href="#adding-additional-fields-to-a-formset" title="Permalink to this headline">¶</a></h2>
<p>If you need to add additional fields to the formset this can be easily
accomplished. The formset base class provides an <tt class="docutils literal"><span class="pre">add_fields</span></tt> method. You
can simply override this method to add your own fields or even redefine the
default fields/attributes of the order and deletion fields:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.forms.formsets</span> <span class="kn">import</span> <span class="n">BaseFormSet</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.forms.formsets</span> <span class="kn">import</span> <span class="n">formset_factory</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">myapp.forms</span> <span class="kn">import</span> <span class="n">ArticleForm</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">BaseArticleFormSet</span><span class="p">(</span><span class="n">BaseFormSet</span><span class="p">):</span>
<span class="gp">... </span>    <span class="k">def</span> <span class="nf">add_fields</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">form</span><span class="p">,</span> <span class="n">index</span><span class="p">):</span>
<span class="gp">... </span>        <span class="nb">super</span><span class="p">(</span><span class="n">BaseArticleFormSet</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">add_fields</span><span class="p">(</span><span class="n">form</span><span class="p">,</span> <span class="n">index</span><span class="p">)</span>
<span class="gp">... </span>        <span class="n">form</span><span class="o">.</span><span class="n">fields</span><span class="p">[</span><span class="s">&quot;my_field&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">forms</span><span class="o">.</span><span class="n">CharField</span><span class="p">()</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">ArticleFormSet</span> <span class="o">=</span> <span class="n">formset_factory</span><span class="p">(</span><span class="n">ArticleForm</span><span class="p">,</span> <span class="n">formset</span><span class="o">=</span><span class="n">BaseArticleFormSet</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">formset</span> <span class="o">=</span> <span class="n">ArticleFormSet</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">form</span> <span class="ow">in</span> <span class="n">formset</span><span class="p">:</span>
<span class="gp">... </span>    <span class="k">print</span><span class="p">(</span><span class="n">form</span><span class="o">.</span><span class="n">as_table</span><span class="p">())</span>
<span class="go">&lt;tr&gt;&lt;th&gt;&lt;label for=&quot;id_form-0-title&quot;&gt;Title:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input type=&quot;text&quot; name=&quot;form-0-title&quot; id=&quot;id_form-0-title&quot; /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
<span class="go">&lt;tr&gt;&lt;th&gt;&lt;label for=&quot;id_form-0-pub_date&quot;&gt;Pub date:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input type=&quot;text&quot; name=&quot;form-0-pub_date&quot; id=&quot;id_form-0-pub_date&quot; /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
<span class="go">&lt;tr&gt;&lt;th&gt;&lt;label for=&quot;id_form-0-my_field&quot;&gt;My field:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input type=&quot;text&quot; name=&quot;form-0-my_field&quot; id=&quot;id_form-0-my_field&quot; /&gt;&lt;/td&gt;&lt;/tr&gt;</span>
</pre></div>
</div>
</div>
<div class="section" id="s-using-a-formset-in-views-and-templates">
<span id="using-a-formset-in-views-and-templates"></span><h2>Using a formset in views and templates<a class="headerlink" href="#using-a-formset-in-views-and-templates" title="Permalink to this headline">¶</a></h2>
<p>Using a formset inside a view is as easy as using a regular <tt class="docutils literal"><span class="pre">Form</span></tt> class.
The only thing you will want to be aware of is making sure to use the
management form inside the template. Let&#8217;s look at a sample view:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.forms.formsets</span> <span class="kn">import</span> <span class="n">formset_factory</span>
<span class="kn">from</span> <span class="nn">django.shortcuts</span> <span class="kn">import</span> <span class="n">render_to_response</span>
<span class="kn">from</span> <span class="nn">myapp.forms</span> <span class="kn">import</span> <span class="n">ArticleForm</span>

<span class="k">def</span> <span class="nf">manage_articles</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
    <span class="n">ArticleFormSet</span> <span class="o">=</span> <span class="n">formset_factory</span><span class="p">(</span><span class="n">ArticleForm</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">request</span><span class="o">.</span><span class="n">method</span> <span class="o">==</span> <span class="s">&#39;POST&#39;</span><span class="p">:</span>
        <span class="n">formset</span> <span class="o">=</span> <span class="n">ArticleFormSet</span><span class="p">(</span><span class="n">request</span><span class="o">.</span><span class="n">POST</span><span class="p">,</span> <span class="n">request</span><span class="o">.</span><span class="n">FILES</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">formset</span><span class="o">.</span><span class="n">is_valid</span><span class="p">():</span>
            <span class="c"># do something with the formset.cleaned_data</span>
            <span class="k">pass</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">formset</span> <span class="o">=</span> <span class="n">ArticleFormSet</span><span class="p">()</span>
    <span class="k">return</span> <span class="n">render_to_response</span><span class="p">(</span><span class="s">&#39;manage_articles.html&#39;</span><span class="p">,</span> <span class="p">{</span><span class="s">&#39;formset&#39;</span><span class="p">:</span> <span class="n">formset</span><span class="p">})</span>
</pre></div>
</div>
<p>The <tt class="docutils literal"><span class="pre">manage_articles.html</span></tt> template might look like this:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="nt">&lt;form</span> <span class="na">method=</span><span class="s">&quot;post&quot;</span> <span class="na">action=</span><span class="s">&quot;&quot;</span><span class="nt">&gt;</span>
    <span class="cp">{{</span> <span class="nv">formset.management_form</span> <span class="cp">}}</span>
    <span class="nt">&lt;table&gt;</span>
        <span class="cp">{%</span> <span class="k">for</span> <span class="nv">form</span> <span class="k">in</span> <span class="nv">formset</span> <span class="cp">%}</span>
        <span class="cp">{{</span> <span class="nv">form</span> <span class="cp">}}</span>
        <span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
    <span class="nt">&lt;/table&gt;</span>
<span class="nt">&lt;/form&gt;</span>
</pre></div>
</div>
<p>However there&#8217;s a slight shortcut for the above by letting the formset itself
deal with the management form:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="nt">&lt;form</span> <span class="na">method=</span><span class="s">&quot;post&quot;</span> <span class="na">action=</span><span class="s">&quot;&quot;</span><span class="nt">&gt;</span>
    <span class="nt">&lt;table&gt;</span>
        <span class="cp">{{</span> <span class="nv">formset</span> <span class="cp">}}</span>
    <span class="nt">&lt;/table&gt;</span>
<span class="nt">&lt;/form&gt;</span>
</pre></div>
</div>
<p>The above ends up calling the <tt class="docutils literal"><span class="pre">as_table</span></tt> method on the formset class.</p>
<div class="section" id="s-manually-rendered-can-delete-and-can-order">
<span id="s-id4"></span><span id="manually-rendered-can-delete-and-can-order"></span><span id="id4"></span><h3>Manually rendered <tt class="docutils literal"><span class="pre">can_delete</span></tt> and <tt class="docutils literal"><span class="pre">can_order</span></tt><a class="headerlink" href="#manually-rendered-can-delete-and-can-order" title="Permalink to this headline">¶</a></h3>
<p>If you manually render fields in the template, you can render
<tt class="docutils literal"><span class="pre">can_delete</span></tt> parameter with <tt class="docutils literal"><span class="pre">{{</span> <span class="pre">form.DELETE</span> <span class="pre">}}</span></tt>:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="nt">&lt;form</span> <span class="na">method=</span><span class="s">&quot;post&quot;</span> <span class="na">action=</span><span class="s">&quot;&quot;</span><span class="nt">&gt;</span>
    <span class="cp">{{</span> <span class="nv">formset.management_form</span> <span class="cp">}}</span>
    <span class="cp">{%</span> <span class="k">for</span> <span class="nv">form</span> <span class="k">in</span> <span class="nv">formset</span> <span class="cp">%}</span>
        <span class="nt">&lt;ul&gt;</span>
            <span class="nt">&lt;li&gt;</span><span class="cp">{{</span> <span class="nv">form.title</span> <span class="cp">}}</span><span class="nt">&lt;/li&gt;</span>
            <span class="nt">&lt;li&gt;</span><span class="cp">{{</span> <span class="nv">form.pub_date</span> <span class="cp">}}</span><span class="nt">&lt;/li&gt;</span>
            <span class="cp">{%</span> <span class="k">if</span> <span class="nv">formset.can_delete</span> <span class="cp">%}</span>
                <span class="nt">&lt;li&gt;</span><span class="cp">{{</span> <span class="nv">form.DELETE</span> <span class="cp">}}</span><span class="nt">&lt;/li&gt;</span>
            <span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>
        <span class="nt">&lt;/ul&gt;</span>
    <span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
<span class="nt">&lt;/form&gt;</span>
</pre></div>
</div>
<p>Similarly, if the formset has the ability to order (<tt class="docutils literal"><span class="pre">can_order=True</span></tt>), it is
possible to render it with <tt class="docutils literal"><span class="pre">{{</span> <span class="pre">form.ORDER</span> <span class="pre">}}</span></tt>.</p>
</div>
<div class="section" id="s-using-more-than-one-formset-in-a-view">
<span id="using-more-than-one-formset-in-a-view"></span><h3>Using more than one formset in a view<a class="headerlink" href="#using-more-than-one-formset-in-a-view" title="Permalink to this headline">¶</a></h3>
<p>You are able to use more than one formset in a view if you like. Formsets
borrow much of its behavior from forms. With that said you are able to use
<tt class="docutils literal"><span class="pre">prefix</span></tt> to prefix formset form field names with a given value to allow
more than one formset to be sent to a view without name clashing. Lets take
a look at how this might be accomplished:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.forms.formsets</span> <span class="kn">import</span> <span class="n">formset_factory</span>
<span class="kn">from</span> <span class="nn">django.shortcuts</span> <span class="kn">import</span> <span class="n">render_to_response</span>
<span class="kn">from</span> <span class="nn">myapp.forms</span> <span class="kn">import</span> <span class="n">ArticleForm</span><span class="p">,</span> <span class="n">BookForm</span>

<span class="k">def</span> <span class="nf">manage_articles</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
    <span class="n">ArticleFormSet</span> <span class="o">=</span> <span class="n">formset_factory</span><span class="p">(</span><span class="n">ArticleForm</span><span class="p">)</span>
    <span class="n">BookFormSet</span> <span class="o">=</span> <span class="n">formset_factory</span><span class="p">(</span><span class="n">BookForm</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">request</span><span class="o">.</span><span class="n">method</span> <span class="o">==</span> <span class="s">&#39;POST&#39;</span><span class="p">:</span>
        <span class="n">article_formset</span> <span class="o">=</span> <span class="n">ArticleFormSet</span><span class="p">(</span><span class="n">request</span><span class="o">.</span><span class="n">POST</span><span class="p">,</span> <span class="n">request</span><span class="o">.</span><span class="n">FILES</span><span class="p">,</span> <span class="n">prefix</span><span class="o">=</span><span class="s">&#39;articles&#39;</span><span class="p">)</span>
        <span class="n">book_formset</span> <span class="o">=</span> <span class="n">BookFormSet</span><span class="p">(</span><span class="n">request</span><span class="o">.</span><span class="n">POST</span><span class="p">,</span> <span class="n">request</span><span class="o">.</span><span class="n">FILES</span><span class="p">,</span> <span class="n">prefix</span><span class="o">=</span><span class="s">&#39;books&#39;</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">article_formset</span><span class="o">.</span><span class="n">is_valid</span><span class="p">()</span> <span class="ow">and</span> <span class="n">book_formset</span><span class="o">.</span><span class="n">is_valid</span><span class="p">():</span>
            <span class="c"># do something with the cleaned_data on the formsets.</span>
            <span class="k">pass</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">article_formset</span> <span class="o">=</span> <span class="n">ArticleFormSet</span><span class="p">(</span><span class="n">prefix</span><span class="o">=</span><span class="s">&#39;articles&#39;</span><span class="p">)</span>
        <span class="n">book_formset</span> <span class="o">=</span> <span class="n">BookFormSet</span><span class="p">(</span><span class="n">prefix</span><span class="o">=</span><span class="s">&#39;books&#39;</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">render_to_response</span><span class="p">(</span><span class="s">&#39;manage_articles.html&#39;</span><span class="p">,</span> <span class="p">{</span>
        <span class="s">&#39;article_formset&#39;</span><span class="p">:</span> <span class="n">article_formset</span><span class="p">,</span>
        <span class="s">&#39;book_formset&#39;</span><span class="p">:</span> <span class="n">book_formset</span><span class="p">,</span>
    <span class="p">})</span>
</pre></div>
</div>
<p>You would then render the formsets as normal. It is important to point out
that you need to pass <tt class="docutils literal"><span class="pre">prefix</span></tt> on both the POST and non-POST cases so that
it is rendered and processed correctly.</p>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../../contents.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Formsets</a><ul>
<li><a class="reference internal" href="#using-initial-data-with-a-formset">Using initial data with a formset</a></li>
<li><a class="reference internal" href="#limiting-the-maximum-number-of-forms">Limiting the maximum number of forms</a></li>
<li><a class="reference internal" href="#formset-validation">Formset validation</a><ul>
<li><a class="reference internal" href="#understanding-the-managementform">Understanding the ManagementForm</a></li>
<li><a class="reference internal" href="#total-form-count-and-initial-form-count"><tt class="docutils literal"><span class="pre">total_form_count</span></tt> and <tt class="docutils literal"><span class="pre">initial_form_count</span></tt></a></li>
<li><a class="reference internal" href="#empty-form"><tt class="docutils literal"><span class="pre">empty_form</span></tt></a></li>
<li><a class="reference internal" href="#custom-formset-validation">Custom formset validation</a></li>
</ul>
</li>
<li><a class="reference internal" href="#validating-the-number-of-forms-in-a-formset">Validating the number of forms in a formset</a><ul>
<li><a class="reference internal" href="#id3"><tt class="docutils literal"><span class="pre">validate_max</span></tt></a></li>
<li><a class="reference internal" href="#validate-min"><tt class="docutils literal"><span class="pre">validate_min</span></tt></a></li>
</ul>
</li>
<li><a class="reference internal" href="#dealing-with-ordering-and-deletion-of-forms">Dealing with ordering and deletion of forms</a><ul>
<li><a class="reference internal" href="#can-order"><tt class="docutils literal"><span class="pre">can_order</span></tt></a></li>
<li><a class="reference internal" href="#can-delete"><tt class="docutils literal"><span class="pre">can_delete</span></tt></a></li>
</ul>
</li>
<li><a class="reference internal" href="#adding-additional-fields-to-a-formset">Adding additional fields to a formset</a></li>
<li><a class="reference internal" href="#using-a-formset-in-views-and-templates">Using a formset in views and templates</a><ul>
<li><a class="reference internal" href="#manually-rendered-can-delete-and-can-order">Manually rendered <tt class="docutils literal"><span class="pre">can_delete</span></tt> and <tt class="docutils literal"><span class="pre">can_order</span></tt></a></li>
<li><a class="reference internal" href="#using-more-than-one-formset-in-a-view">Using more than one formset in a view</a></li>
</ul>
</li>
</ul>
</li>
</ul>

  <h3>Browse</h3>
  <ul>
    
      <li>Prev: <a href="index.html">Working with forms</a></li>
    
    
      <li>Next: <a href="modelforms.html">Creating forms from models</a></li>
    
  </ul>
  <h3>You are here:</h3>
  <ul>
      <li>
        <a href="../../index.html">Django 1.7.8.dev20150401230226 documentation</a>
        
          <ul><li><a href="../index.html">Using Django</a>
        
          <ul><li><a href="index.html">Working with forms</a>
        
        <ul><li>Formsets</li></ul>
        </li></ul></li></ul>
      </li>
  </ul>

  <h3>This Page</h3>
  <ul class="this-page-menu">
    <li><a href="../../_sources/topics/forms/formsets.txt"
           rel="nofollow">Show Source</a></li>
  </ul>
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Apr 02, 2015</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    &laquo; <a href="index.html" title="Working with forms">previous</a>
     |
    <a href="../index.html" title="Using Django" accesskey="U">up</a>
   |
    <a href="modelforms.html" title="Creating forms from models">next</a> &raquo;</div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>